package com.lz.controller.lg;

import com.alibaba.fastjson.JSON;
import com.lz.entity.Employee;
import com.lz.repository.lg.VO.MortgageVO;
import com.lz.service.lg.MortgageSheetServicelg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import util.PageUitl;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;

@Controller
public class MortgageSheetControllerlg {
	@Autowired
	private MortgageSheetServicelg mortgageSheetServicelg;
	
	//跳转到新增按揭单界面
	@RequestMapping("/turnToMortgageSheet")
	public String turnToMortgageSheet(Model model){
		Integer integer = mortgageSheetServicelg.selectMaxId();
		model.addAttribute("maxId",integer+1);
		return "lgaddMortgage";
	}
	
	//增加按揭单
	@RequestMapping("/addMortgageSheet")
	public String addMortgageSheet(MortgageVO mortgageVO, Model model, HttpServletRequest request){
		mortgageSheetServicelg.addMortgage(mortgageVO);
		Employee employee = (Employee)request.getSession().getAttribute("employee");
		model.addAttribute("employee2",employee);
		return "redirect:/selectMortgage";
	}
	// 跳转到查询页面
	@RequestMapping("/selectMortgage")
	public String selectMortgageSheet(String customerName,String bankName,Model model){
		HashMap<String, String> map = new HashMap<>();
		map.put("customer.customerName", customerName);
		map.put("bank.bankName", bankName);
		Integer rows = mortgageSheetServicelg.mortgageRows(map);
		int pageSize = 3;
		PageUitl<Object> pageUitl = new PageUitl<>(1, pageSize, rows);
		List<MortgageVO> list = mortgageSheetServicelg.mortgageList(map, 1, pageSize);
		model.addAttribute("list", list);
		model.addAttribute("pageNo",pageUitl.getPageNo());
		model.addAttribute("pageTotal",pageUitl.getPageTotal());
		return "lgselectMortgage";
	}
	
	//根据按揭单主键查询到按揭单信息
	@RequestMapping("/alertMortgage")
	public String alertMortgage(String mortgageSheetNo,Model model){
		MortgageVO mortgageVO = mortgageSheetServicelg.selectByPK(Integer.parseInt(mortgageSheetNo));
		if(!mortgageVO.getMortgageSheetState().equals("已保存") || !mortgageVO.getMortgageSheetState().equals("审核驳回")){
			model.addAttribute("hidden","hidden");
		}else {
			model.addAttribute("hidden","no");
		}
		model.addAttribute("mortgageVO",mortgageVO);
		return "mortgageDetails";
	}

	//保存修改后的按揭单
	@RequestMapping("/changeMortgageSheet")
	public String changeMortgageSheet(MortgageVO mortgageVO){
		mortgageSheetServicelg.changeMortgageSheet(mortgageVO);
		return "redirect:/selectMortgage";
	}

	//上一页,下一页
	@RequestMapping(value = "/selectMortgageByJson",produces = {"application/json;charset=utf-8"})
	@ResponseBody
	public String selectMortgageByJson(String customerName,String bankName ,Integer pageNo){
		HashMap<String, String> map = new HashMap<>();
		map.put("customer.customerName", customerName);
		map.put("bank.bankName", bankName);
		Integer rows = mortgageSheetServicelg.mortgageRows(map);
		int pageSize = 3;
		PageUitl<Object> pageUitl = new PageUitl<>(pageNo, pageSize, rows);
		List<MortgageVO> list = mortgageSheetServicelg.mortgageList(map, pageUitl.getPageNo(), pageUitl.getPageSize());
		HashMap<String, Object> map1 = new HashMap<>();
		map1.put("list",list);
		map1.put("pageNo",pageUitl.getPageNo());
		map1.put("pageTotal",pageUitl.getPageTotal());
		return JSON.toJSONString(map1);
	}

	//删除条数
	@RequestMapping("/deleteMortgage")
	public String deleteMortgage(Integer mortgageSheetNo){
		mortgageSheetServicelg.byPK(mortgageSheetNo);
		return "redirect:/selectMortgage";
	}

	//提交按揭单
	@RequestMapping("/submitMortage")
	public String submitMortage(Integer mortgageSheetNo){
		mortgageSheetServicelg.submitMortgage(mortgageSheetNo);
		return "redirect:/selectMortgage";
	}
}
